/*
 * @desc:
 * @Author: 钟强
 * @Date: 2021-12-22 15:32:29
 * @LastEditors: 钟强
 * @LastEditTime: 2021-12-22 16:11:47
 */
import store from '../store'

function checkPermission (el, binding) {
  // 获取绑定的值，此处为权限
  const { value } = binding

  // 获取所有的按钮权限数据
  const points = store.getters.userInfo.permission.points

  if (value && value instanceof Array) {
    // 当出入的指令集是一个数组时

    // 匹配对应的指令
    const hasPermission = points.some(point => {
      return value.includes(point)
    })

    if (!hasPermission) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  } else {
    throw new Error('v-permission value is ["admin","editor"]')
  }
}

export default {
  mounted (el, binding) {
    checkPermission(el, binding)
  },
  update (el, binding) {
    checkPermission(el, binding)
  }
}
